package io.renren.modules.project.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.annotation.SysLog;
import io.renren.modules.project.entity.ProjectContractEntity;
import io.renren.modules.project.service.*;
import io.renren.modules.project.vo.RecycleVoEntity;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.project.entity.*;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 项目回收站列表
 *
 * @author ygg
 * @date 2019-10-31 11:36:02
 */
@RestController
@RequestMapping("project/recycle")
public class RecycleController {
    @Autowired
    private RecycleService recycleService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectPlanService projectPlanService;

    @Autowired
    private ProjectScheduleService projectScheduleService;

    @Autowired
    private ProjectWorkService projectWorkService;

    @Autowired
    private ProjectGroupService projectGroupService;

    @Autowired
    private CheckOutputService checkOutputService;

    @Autowired
    private CheckQualityService checkQualityService;

    @Autowired
    private ProjectSituationService projectSituationService;

    @Autowired
    private QualityScoreService qualityScoreService;

    @Autowired
    private ProjectContractService projectContractService;

    @Autowired
    private QualityScoreRadioService qualityScoreRadioService;

    @Autowired
    private CheckOutputTempService checkOutputTempService;

    @Autowired
    private CheckOutputRemarkService checkOutputRemarkService;

    @Autowired
    private BackWorkService backWorkService;

    @Autowired
    private ProjectBillService projectBillService;

    @Autowired
    private ProjectMtaskService projectMtaskService;

    @Autowired
    private ProjectAuthachService projectAuthachService;

    @Autowired
    private ProjectExpachService projectExpachService;

    @Autowired
    private QualityOutputService qualityOutputService;

    /**
     * 列表
     */
//    @SysLog("查看项目回收站列表")
    @RequestMapping("/list")
    @RequiresPermissions("project:recycle:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = recycleService.getRecyclePage(params);

        return R.ok().put("page", page);
    }



    /**
     * 恢复项目
     */
    @SysLog("恢复项目")
    @RequestMapping("/update")
    @RequiresPermissions("project:recycle:restore")
    public R update(@RequestBody ProjectEntity project){
        ProjectContractEntity contractEntity = projectContractService.getByContractNo(project.getContractNo());
        if( contractEntity == null){
            return R.error("该项目所关联的合同不存在了，无法恢复！");
        }

        project.setPStage("1");
        projectService.updateById(project);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("彻底删除回收站项目")
    @RequestMapping("/delete")
    @RequiresPermissions("project:recycle:delete")
    public R delete(@RequestBody Map<String, Object> params){
        if(!StringUtils.isNotBlank((String) params.get("projectNo"))){
            return R.error("项目编号为空！");
        }
        //项目 进度 工作安排 工作组 产值 质检及其评分 项目情况 成果清单 返修记录相关内容删除
        projectService.delete(new EntityWrapper<ProjectEntity>().in("project_no", (String) params.get("projectNo")));
        projectPlanService.delete(new EntityWrapper<ProjectPlanEntity>().in("project_no", (String) params.get("projectNo")));
        projectScheduleService.delete(new EntityWrapper<ProjectScheduleEntity>().in("project_no", (String) params.get("projectNo")));
        projectWorkService.delete(new EntityWrapper<ProjectWorkEntity>().in("project_no", (String) params.get("projectNo")));
        projectGroupService.delete(new EntityWrapper<ProjectGroupEntity>().in("project_no", (String) params.get("projectNo")));
        checkOutputService.delete(new EntityWrapper<CheckOutputEntity>().in("project_no", (String) params.get("projectNo")));
        checkQualityService.delete(new EntityWrapper<CheckQualityEntity>().in("project_no", (String) params.get("projectNo")));
        projectSituationService.delete(new EntityWrapper<ProjectSituationEntity>().in("project_no", (String) params.get("projectNo")));
        qualityScoreService.delete(new EntityWrapper<QualityScoreEntity>().in("project_no", (String) params.get("projectNo")));
        qualityScoreRadioService.delete(new EntityWrapper<QualityScoreRadioEntity>().in("project_no", (String) params.get("projectNo")));
        checkOutputTempService.delete(new EntityWrapper<CheckOutputTempEntity>().in("project_no", (String) params.get("projectNo")));
        backWorkService.delete(new EntityWrapper<BackWorkEntity>().in("project_no", (String) params.get("projectNo")));
        checkOutputRemarkService.delete(new EntityWrapper<CheckOutputRemarkEntity>().in("project_no", (String) params.get("projectNo")));
        projectBillService.delete(new EntityWrapper<ProjectBillEntity>().in("project_no", (String) params.get("projectNo")));
        projectMtaskService.delete(new EntityWrapper<ProjectMtaskEntity>().in("project_no", (String) params.get("projectNo")));
        projectAuthachService.delete(new EntityWrapper<ProjectAuthachEntity>().in("project_no", (String) params.get("projectNo")));
        projectExpachService.delete(new EntityWrapper<ProjectExpachEntity>().in("project_no", (String) params.get("projectNo")));
        qualityOutputService.delete(new EntityWrapper<QualityOutputEntity>().in("project_no", (String) params.get("projectNo")));
        return R.ok();
    }

}